/**
 * PluginList.java
 */
package com.pluginsacceptor.acceptor.activator;

import java.util.ArrayList;
import java.util.List;

import com.pluginsacceptor.acceptor.activator.model.PluginInfo;

/**
 * The class PluginList intended to contain a list of added plug-ins (plug-ins storage list)
 */
public class PluginList {

    /** List of active plug-ins */
    private static final List<PluginInfo> activePlugins = new ArrayList<PluginInfo>();

    /**
     * Add plug-in to list
     * @param pluginInfo the information about plugin
     */
    public static void addPlugin(final PluginInfo pluginInfo) {
        activePlugins.add(pluginInfo);
    }

//    /**
//     * Remove plug-in to list
//     * @param pluginInfo the information about plugin
//     */
//    public static void removePlugin(final PluginInfo pluginInfo) {
//        activePlugins.remove(pluginInfo);
//    }

    /**
     * Return <code>true</code> if plug-in contains in list
     * @param name the plug-in name
     */
    public static boolean contains(final String name) {

        boolean result = false;
        for (final PluginInfo pluginInfo : activePlugins) {
            if (pluginInfo.getName().equals(name)) {
                result = true;
                break;
            }
        }
        return result;
    }

    /**
     * Get plug-in instance for name
     * @param name the plugin name
     */
    public static PluginInfo getPluginInfo(final String name) {

        for (final PluginInfo pluginInfo : activePlugins) {
            if (pluginInfo.getName().equals(name)) {
                return pluginInfo;
            }
        }

        throw new UnsupportedOperationException("Plugin '" + name + "' is absent");
    }

    /**
     * @return the activeplugins
     */
    public static List<PluginInfo> getActivePlugins() {
        return activePlugins;
    }

    /**
     * @param pluginInfo the desired plugin info
     * @return <code>true</code> if child pluging exist for desired plugin info
     */
    public static boolean isLinkedPluginExist(final PluginInfo pluginInfo) {
        boolean result = false;

        for (final PluginInfo plug : PluginList.getActivePlugins()) {
            if (!plug.equals(pluginInfo)) {
                for (final String child : plug.getRequirements()) {
                    if (pluginInfo.getName().equals(child)) {
                        result = true;
                        break;
                    }
                }
            }

            if (result) {
                break;
            }
        }
        return result;
    }
}
